﻿namespace Microsoft.VisualStudio.TextTemplating.CommandLine
{
    using System;
    using System.IO;

    internal abstract class Command : IDisposable
    {
        private TextWriter commandError;
        private TextWriter commandOut;
        private int returnCode;
        private CommandSpecification specification;

        protected Command()
        {
        }

        public virtual void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        public void Load(CommandOptionCollection options)
        {
            this.LoadImplementation(options);
        }

        protected virtual void LoadImplementation(CommandOptionCollection options)
        {
        }

        public void Run()
        {
            if (this.commandOut == null)
            {
                throw new CommandException("No output writer has been given for the command.");
            }
            this.RunImplementation();
        }

        protected abstract void RunImplementation();

        public TextWriter Error
        {
            get
            {
                return this.commandError;
            }
            set
            {
                this.commandError = value;
            }
        }

        public TextWriter Output
        {
            get
            {
                return this.commandOut;
            }
            set
            {
                this.commandOut = value;
            }
        }

        public int ReturnCode
        {
            get
            {
                return this.returnCode;
            }
            set
            {
                this.returnCode = value;
            }
        }

        public CommandSpecification Specification
        {
            get
            {
                return this.specification;
            }
            set
            {
                this.specification = value;
            }
        }
    }
}

